#!/bin/sh
# Script to start the httpdEx server

# Check for the existence of the PID file. If present, extract 
# the PID and check whether a process with that pid is running 
# or not. If running, initimate console, else start the server.

MEMLOG="memsys.log"
SERVERROOT="/home/testimp/httpdex"
PIDPATH="$SERVERROOT/bin"
PIDFILE="httpdEx.pid"
EXECPATH="$SERVERROOT/bin"
VERSION="1.0"
PIDABS=$PIDPATH/$PIDFILE

if [ -e $PIDABS ]
then
   SRVPID=`cat $PIDABS`
   
   isrunning=`ps -eax|sed --expression="s/^ *//g"|cut -d " " -f1|grep $SRVPID`
   
   if [ -z $isrunning ]
   then 
      # remove the old pid file
      echo "Attempting to start httpdEx $VERSION"
   else
      echo "httpdEx $VERSION already running [$SRVPID]"
      exit -1
   fi
else
   echo "Attempting to start httpdEx $VERSION"
   echo "here"
fi       

# Start the server as a background job
CURRDIR=$PWD
cd $EXECPATH
./httpdex 2>./$MEMLOG &
cd $CURRDIR

# Wait till the server starts up
sleep 1

# Retrieve the PID
if [ -e $PIDABS ]; then
   SRVPID=`cat $PIDABS`
   if [ -z SRVPID ]; then
      echo "Failed to start server"
   else
      echo "Server started [$SRVPID]"   
   fi
fi   

# End of File
